package call.count;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
// * 功能：统计结果广播事件。固定周期会通过该事件进行广播
 * 把统计结果打印到日志中
 *
 * @author baitao20
 * @date 2024/2/20 21:46
 **/
public class CallCountBroadcastEventLog implements CallCountBroadcastEvent {

    private final static Logger logger = LoggerFactory.getLogger(CallCountBootStrap.class);

    @Override
    public void onEvent(Collection<CallCountResult> callCountClassList) {
        this.timerPrintCallCountResult(callCountClassList);
    }

    private String timerPrintCallCountResult(Collection<CallCountResult> callCountClassList) {
        String currentDateTimeString = CallCountDateUtils.getNowDateString();
        List<String> allCallCount = callCountClassList.stream()
                .sorted(Comparator.comparing(CallCountResult::getCountSorted).reversed())
                .map(method -> String.join(CallCountConstant.SPLIT_CHAR, method.getClassName(), method.getMethodName(), COMPONENT_TYPE_ENUM.getLabelByValue(method.getType()), method.getCount().toString()))
                .collect(Collectors.toList());
        logger.info("[CALL_COUNT_LOG] CallCountBroadcastEventLog datetime:{} 调用统计:{}", currentDateTimeString, CallCountStringUtils.collectionToString(allCallCount));
        return currentDateTimeString;
    }

}